package com.education.welco.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.core.BaseController;
import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.FileGetSizeUtil;
import com.education.welco.pojo.*;
import com.education.welco.service.ILPaperQueRelationService;
import com.education.welco.service.ILPaperService;
import com.education.welco.service.ILQuestionsService;
import com.education.welco.service.ILResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 课程资源 信息操作处理
 *
 * @author d
 * @date 2020-05-08
 */
@RestController
@RequestMapping("/lResources")
public class LResourcesController extends BaseController {

    @Autowired
    private ILResourcesService resourcesService;
    @Autowired
    private ILQuestionsService questionsService;
    @Autowired
    private ILPaperService paperService;
    @Autowired
    private ILPaperQueRelationService paperQueRelationService;

    /**
     *  根据平台课程 以及 分类查找出对应的资源列表
     */
    @GetMapping("/list")
    private ApiReturnObject list(Integer catalogId,String type,Integer pageNum,Integer pageSize){
        if(catalogId != null && type != null && !"".equals(type)){
            LResources resources = new LResources();
            resources.setRCatalogid(catalogId);
            resources.setType(type);
            if(pageNum != null && pageSize != null ){
                PageHelper.startPage(pageNum,pageSize);
            }
            List<LResources> lResources = resourcesService.selectLResourcesList(resources);
            if ("8".equals(type)) {
                for (LResources lResource : lResources) {
                    if (lResource.getRUrl() != null && !"".equals(lResource.getRUrl())) {
                        if (lResource.getRUrl().contains(",")) {
                            //true 切割
                            String[] split = lResource.getRUrl().split(",");
                            if (split.length > 1) {
                                //两个路径都有 上传文件map 3d集合
                                lResource.setThreeUrl(split[0]);
                                //上传文件map flash集合
                                lResource.setFlashUrl(split[1]);

                            } else if(split.length == 1){
                                // 3d
                                //上传文件map 3d集合
                                lResource.setThreeUrl(split[0]);
                            }
                        } else {
                            //flash
                            lResource.setFlashUrl(lResource.getRUrl());
                        }
                    }
                }
            }
            PageInfo<LResources> pageInfo = new PageInfo<>(lResources);
            return new ApiReturnObject("200","查询成功",pageInfo);
        }
        return new ApiReturnObject("201","缺少参数","");
    }


    /**
     *  模拟测试
     */
    @GetMapping("/testList")
    public ApiReturnObject testList(Integer catalogId,Integer pageNum,Integer pageSize){
        if(catalogId != null ){
            //catalogId 查询出试卷  查出试题列表
            if(pageNum != null && pageSize != null ){
                PageHelper.startPage(pageNum,pageSize);
            }
            //试卷列表
            LPaper paper = new LPaper();
            paper.setCatalogId(catalogId);
            List<LPaper> lPapers = paperService.selectLPaperList(paper);
            PageInfo<LPaper> pageInfo = new PageInfo<>(lPapers);
            return new ApiReturnObject("200","查询成功",pageInfo);
        }
        return new ApiReturnObject("201","缺少catalogId","");
    }

    /**
     *  添加时选择试卷列表 做添加
     */
    @GetMapping("paperList")
    public ApiReturnObject paperList(Integer catalogId){
        if(catalogId != null){
            LPaper lPaper = new LPaper();
            lPaper.setCatalogId(catalogId);
            List<LPaper> lPapers = paperService.selectLPaperList(lPaper);
            return new ApiReturnObject("200","查询成功",lPapers);
        }
       return new ApiReturnObject("200","缺少参数","");
    }


    /**
     *  远程调用 添加3d 和 flash
     */
    @GetMapping("/videoAdd")
    public ApiReturnObject videoAdd(Integer id,String type,String videoUrl,String fileName){
        //查询出虚拟教学 type 8
        LResources resources = resourcesService.selectLResourcesById(id);
        if(resources.getRUrl() == null){
            resources.setRUrl("");
        }
        String i = "6";
        String i1 = "7";
        if(i.equals(type)){
            //3d
            resources.setRUrl(videoUrl + "," + resources.getRUrl() );
        }
        if(i1.equals(type)){
            //flash
            resources.setRUrl(resources.getRUrl() + videoUrl );
        }
        //设置虚拟教学的url
        resourcesService.updateLResources(resources);
        LResources resources1 = new LResources();
        //进行添加
        resources1.setRName(resources.getRName());
        resources1.setRBrowsing(0);
        resources1.setRUrl(videoUrl);
        resources1.setType(type);
        resources1.setResIco(resources.getResIco());
        //获取文件大小
        String qiNiuUrl2 = FileGetSizeUtil.getQiNiuUrl2(videoUrl);
        resources1.setRSize(qiNiuUrl2);
        if(resources.getRCatalogid() != null){
            resources1.setRCatalogid(resources.getRCatalogid());
        }
        resources1.setRNumber(id.toString());
        //编辑状态
        resources1.setCreateTime(new Date());
        resourcesService.insertLResources(resources1);
        return new ApiReturnObject("200","添加成功","");
    }



    /**
     *  添加虚拟教学
     */
    @PostMapping("/addVirtual")
    public ApiReturnObject addVirtual(@RequestBody LResources resources) {
        //往资源表添加数据
        resources.setRBrowsing(0);
        resources.setType("8");
        resources.setCreateTime(new Date());
        resourcesService.insertLResources(resources);
        //添加 3D
        if(resources.getThreeUrl() != null && !"".equals(resources.getThreeUrl())){
            resources.setRUrl(resources.getThreeUrl() + "," );
            LResources resources1 = resources;
            resources1.setRName(resources.getRName());
            resources1.setRBrowsing(0);
            resources1.setRUrl(resources1.getThreeUrl());
            resources1.setType("6");
            //获取文件大小
            String qiNiuUrl2 = FileGetSizeUtil.getQiNiuUrl2(resources1.getThreeUrl());
            resources1.setRSize(qiNiuUrl2);
            if(resources1.getRCatalogid() != null){
                resources1.setRCatalogid(resources1.getRCatalogid());
            }
            resources1.setRNumber(resources.getId().toString());
            //编辑状态
            resources1.setCreateTime(new Date());
            resourcesService.insertLResources(resources1);
        }
        //添加 flash
        if(resources.getFlashUrl() != null && !"".equals(resources.getFlashUrl())){
            resources.setRUrl(resources.getRUrl() + resources.getFlashUrl());
            LResources resources2 = resources;
            resources2.setRName(resources.getRName());
            resources2.setRBrowsing(0);
            resources2.setRUrl(resources2.getFlashUrl());
            resources2.setType("7");
            //获取文件大小
            String qiNiuUrl2 = FileGetSizeUtil.getQiNiuUrl2(resources2.getFlashUrl());
            resources2.setRSize(qiNiuUrl2);
            if(resources2.getRCatalogid() != null){
                resources2.setRCatalogid(resources2.getRCatalogid());
            }
            resources2.setRNumber(resources.getId().toString());
            //编辑状态
            resources2.setCreateTime(new Date());
            resourcesService.insertLResources(resources2);
        }
        return new ApiReturnObject("200","添加成功","");
    }



    /**
     *  上传文件及添加数据
     */
    @GetMapping("/add2")
    public ApiReturnObject add2(String fileName,String fileUrl,String type,Integer catalogId){
        //进行添加
        LResources resources = new LResources();
        resources.setRName(fileName);
        resources.setRUrl(fileUrl);
        resources.setType(type);
        String i = "4";
        if(i.equals(type)){
            resources.setResIco(fileUrl);
        }
        resources.setRCatalogid(catalogId);
        resources.setCreateTime(new Date());
        resourcesService.insertLResources(resources);
        return new ApiReturnObject("200","上传并添加成功","");
    }

    /**
     *  删除资源
     */
    @PostMapping("/del")
    public ApiReturnObject del(@RequestBody LResources resources){
        if(resources.getId() != null ){
            //根据id找到类型
            LResources resources1 = resourcesService.selectLResourcesById(resources.getId());
            if("8".equals(resources1.getType())){
                //删除类型为 6 7 的资源
                LResources resources2 = new LResources();
                resources2.setRNumber(resources.getId().toString());
                resources2.setType("6");
                List<LResources> lResources = resourcesService.selectLResourcesList(resources2);
                if(lResources != null && lResources.size() > 0){
                    for (LResources lResource : lResources) {
                        if(!"".equals(lResource.getRUrl())){
                            QiniuUtil.deleteFileFromQiniu(lResource.getRUrl());
                        }
                        resourcesService.deleteLResourcesById(lResource.getId());
                    }
                }

                resources2.setType("7");
                List<LResources> lResources2 = resourcesService.selectLResourcesList(resources2);
                if(lResources2 != null && lResources2.size() > 0){
                    for (LResources lResource : lResources2) {
                        if(!"".equals(lResource.getRUrl())){
                            QiniuUtil.deleteFileFromQiniu(lResource.getRUrl());
                        }
                        resourcesService.deleteLResourcesById(lResource.getId());
                    }
                }
            }else {
                if(!"".equals(resources1.getRUrl())){
                    QiniuUtil.deleteFileFromQiniu(resources1.getRUrl());
                }
            }
            resourcesService.deleteLResourcesById(resources.getId());
            return new ApiReturnObject("200","删除成功","");
        }
        return new ApiReturnObject("201","缺少参数id","");
    }

    /**
     *  删除试题
     */
    @PostMapping("/delQues")
    public ApiReturnObject delQues(@RequestBody LQuestions questions){
        if(questions.getId() != null ){
            //删除试题
            questionsService.deleteLQuestionsById2(questions.getId());
            //删除试卷
            paperService.deleteLPaperById(questions.getId());
            return new ApiReturnObject("200","删除成功","");
        }
        return new ApiReturnObject("201","缺少参数id","");
    }


    /**
     *  删除资源  3d flash
     */
    @PostMapping("/del3dFlash")
    public ApiReturnObject del3dFlash(@RequestBody LResources resources){
        if(!"".equals(resources.getType())){
            if("6".equals(resources.getType()) ){
                //删除3D  修改虚拟教学的url
                LResources resources1 = resourcesService.selectLResourcesById(resources.getId());
                //修改url
                if(resources1.getRUrl().contains(",")){
                    // 3d flash 都有
                    String[] split = resources1.getRUrl().split(",");
                    if(split.length > 1){
                        // 3d flash 都有
                        resources1.setRUrl(split[1]);
                    }else {
                        resources1.setRUrl("");
                    }
                }
                resourcesService.updateLResources(resources1);
                //删除6资源
                LResources res = new LResources();
                res.setRNumber(resources.getId().toString());
                res.setType(resources.getType());
                List<LResources> lResources = resourcesService.selectLResourcesList(res);
                if(lResources != null && lResources.size() > 0 ){
                    for (LResources lResource : lResources) {
                        if(!"".equals(lResource.getRUrl())){
                            QiniuUtil.deleteFileFromQiniu(lResource.getRUrl());
                        }
                        resourcesService.deleteLResourcesById(lResource.getId());
                    }
                }
                return new ApiReturnObject("200","删除成功","");
            }

            if("7".equals(resources.getType()) ){
                //删除flash  修改虚拟教学的url
                LResources resources1 = resourcesService.selectLResourcesById(resources.getId());
                //修改url
                if(resources1.getRUrl().contains(",")){
                    // 3d flash 都有
                    String[] split = resources1.getRUrl().split(",");
                    if(split.length > 1){
                        // 3d flash 都有
                        resources1.setRUrl(split[0]);
                    }else {
                        resources1.setRUrl("");
                    }
                }
                resources1.setRUrl("");
                resourcesService.updateLResources(resources1);
                //删除7资源
                LResources res = new LResources();
                res.setRNumber(resources.getId().toString());
                res.setType(resources.getType());
                List<LResources> lResources = resourcesService.selectLResourcesList(res);
                if(lResources != null && lResources.size() > 0 ){
                    for (LResources lResource : lResources) {
                        if(!"".equals(lResource.getRUrl())){
                            QiniuUtil.deleteFileFromQiniu(lResource.getRUrl());
                        }
                        resourcesService.deleteLResourcesById(lResource.getId());
                    }
                }
                return new ApiReturnObject("200","删除成功","");
            }
        }
        return new ApiReturnObject("201","缺少参数id","");
    }

    public static void main(String[] args) {
        String qiNiuUrl1 = FileGetSizeUtil.getQiNiuUrl2("1596101139237.png");
        String qiNiuUrl2 = FileGetSizeUtil.getQiNiuUrl2("1596102018333.png");
        System.out.println(qiNiuUrl1);
        System.out.println(qiNiuUrl2);
    }

}